package com.hd.shiro2204.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.shiro2204.dao.UserDao;
import com.hd.shiro2204.entity.*;
import com.hd.shiro2204.service.*;
import com.hd.shiro2204.utils.Constants;
import com.hd.shiro2204.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Autowired
    private UserDao userDAO;
    @Autowired
    RoleService roleService;

    @Autowired
    PermsService permsService;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    RolePermsService rolePermsService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(String username,String password) {
        //1.查询当前用产名在数据库中是否存在，如果存在就楚异常
        User dbuser = this.getOne(new QueryWrapper<User>().eq("username", username));
        if (dbuser!=null){
            throw new RuntimeException("用户名已存在");
        }
        //2.处理密码
        //1.生成随机盐
        String salt = SaltUtils.getSalt(Constants.SALT_LENTH);
        //2.将随机盐保存到数据
        //3.明文密码进行md5 + salt + hash散列
        String md5Hash = new Md5Hash(password,salt,Constants.HASH_TINES).toHex();
        User user = new User();
        user.setUsername(username);
        user.setSalt(salt);
        user.setPassword(md5Hash);
        this.save(user);

    }
    @Override
    public User findByUserName(String username) {
        return this.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
    }
    @Override
    public List<String> findRoles(User user) {

        final List<UserRole> list = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId()));

        final ArrayList<String> result = new ArrayList<>();

        list.forEach(userRole -> {
            final Role role = roleService.getById(userRole.getRoleId());
            result.add(role.getName());
        });

        return result;
    }

    @Override
    public List<String> findPermissions(User user) {

        final List<UserRole> userRoles = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId()));


        final ArrayList<String> result = new ArrayList<>();

        userRoles.forEach(userRole -> {
            final List<RolePerms> rolePerms = rolePermsService.list(new LambdaQueryWrapper<RolePerms>().eq(RolePerms::getRoleId, userRole.getRoleId()));
            rolePerms.forEach(rp -> {
                final Perms pers = permsService.getById(rp.getPermsId());
                result.add(pers.getName());
            });
        });

        return result;
    }
}
